﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;
using System.IO;
using System.Xml.Linq;
using System.Diagnostics;


namespace NETCommon.Files
{
    public sealed class NConfig
    {
        #region Singleton
        private static readonly NConfig TISGlobal = new NConfig();

        public static NConfig Instance
        {
            get { return TISGlobal; }
        }

        #endregion
        private int m_nSvrCount = 0;
        private NSimpleIniFile m_iniCfg = new NSimpleIniFile(NConfigDefine.g_configfile);
        private NConfig()
        {
        }
        public string ServerAddress(int nHostId)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_hostaddr_key, nHostId);
            return m_iniCfg.IniReadValue(NConfigDefine.g_tis_section, keyStr);
        }
        public void ServerAddressSet(int nHostId, string strAddr)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_hostaddr_key, nHostId);
            m_iniCfg.IniWriteValue(NConfigDefine.g_tis_section, keyStr, strAddr);
        }
        public string ServerName(int nHostId)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_hostname_key, nHostId);
            return m_iniCfg.IniReadValue(NConfigDefine.g_tis_section, keyStr);
        }
        public void ServerNameSet(int nHostId, string strName)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_hostname_key, nHostId);
            m_iniCfg.IniWriteValue(NConfigDefine.g_tis_section, keyStr, strName);
        }
        public string Database(int nHostId)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_db_key, nHostId);
            return m_iniCfg.IniReadValue(NConfigDefine.g_tis_section, keyStr);
        }
        public void DatabaseSet(int nHostId, string strDBName)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_db_key, nHostId);
            m_iniCfg.IniWriteValue(NConfigDefine.g_tis_section, keyStr, strDBName);
        }
        public string User(int nHostId)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_dbuser_key, nHostId);
            return m_iniCfg.IniReadValue(NConfigDefine.g_tis_section, keyStr);
        }
        public void UserSet(int nHostId, string strUser)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_dbuser_key, nHostId);
            m_iniCfg.IniWriteValue(NConfigDefine.g_tis_section, keyStr, strUser);
        }
        public string Password(int nHostId)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_dbpw_key, nHostId);
            return m_iniCfg.IniReadValue(NConfigDefine.g_tis_section, keyStr);
        }
        public void PasswordSet(int nHostId, string strPwd)
        {
            string keyStr = string.Format("{0}{1}", NConfigDefine.g_dbpw_key, nHostId);
            m_iniCfg.IniWriteValue(NConfigDefine.g_tis_section, keyStr, strPwd);
        }
        public string AdminPwd()
        {
            return m_iniCfg.IniReadValue(NConfigDefine.g_admin_section, "PWD");
        }
        public string AdminUser()
        {
            return m_iniCfg.IniReadValue(NConfigDefine.g_admin_section, "USER");
        }
        public string TisServerName()
        {
            return m_iniCfg.IniReadValue(NConfigDefine.g_tis_section, NConfigDefine.g_svrname_key);
        }
        public int TisLogging()
        {
            return Convert.ToInt32(m_iniCfg.IniReadValue(NConfigDefine.g_omsset_section, NConfigDefine.g_omsset_logging));
        }
        public void TisServerNameSet(string strVal)
        {
            m_iniCfg.IniWriteValue(NConfigDefine.g_tis_section, NConfigDefine.g_svrname_key, strVal);
        }
        public string SysType()
        {
            return m_iniCfg.IniReadValue(NConfigDefine.g_system_section, NConfigDefine.g_type_key);
        }
        public void DeletAllKeys(string strSection)
        {
            m_iniCfg.IniWriteValue(strSection, null, "");
        }
        public int ServerCount
        {
            get
            {
                string tmpStr = m_iniCfg.IniReadValue(NConfigDefine.g_tis_section, NConfigDefine.g_hostcount_key);
                m_nSvrCount = Convert.ToInt32(tmpStr);
                return m_nSvrCount;
            }
            set { m_nSvrCount = value; }
        }
        public void ServerCountSet(int nHostCnt)
        {
            m_iniCfg.IniWriteValue(NConfigDefine.g_tis_section, NConfigDefine.g_hostcount_key, nHostCnt.ToString());
        }
    }
}
